package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqXzqArea;
import cloud.maque.admin.domain.entity.MqXzqAreaRelation;
import cloud.maque.admin.domain.vo.MqXzqAreaVo;
import cloud.maque.admin.mapper.MqXzqAreaMapper;
import cloud.maque.admin.service.MqXzqAreaService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 4color
 * @description mq_xzq_area
 * @date 2023-03-17
 */
@Service
public class MqXzqAreaServiceImpl extends ServiceImpl<MqXzqAreaMapper, MqXzqArea> implements MqXzqAreaService {

    @Autowired
    MqXzqAreaServiceImplHelper helper;


    @Autowired
    MqXzqAreaRelationServiceImpl relationService;

    @Override
    public List<MqXzqAreaVo> xzqAreaTree(String areaCode, String parentId, String areaId, Boolean isAll, Boolean fromredis) {


        String rediskey = helper.GetReidsKey(areaCode, areaId, isAll);
        List<MqXzqAreaVo> tree = helper.GetTreeRedis(fromredis, rediskey);

        if (fromredis) {
            if (tree != null) {
                return tree;
            }
        }

        List<MqXzqArea> result = null;
        if (!StrUtil.isEmptyIfStr(parentId)) {
            result = helper.listByParentId(parentId);
        } else if (!StrUtil.isEmptyIfStr(areaId)) {
            result = helper.listByAreaId(areaId);
        } else {
            result = helper.listByCode(areaCode);
        }
        //拷贝
        List<MqXzqAreaVo> xzqTrees = result.stream().map(s -> {
            MqXzqAreaVo t = new MqXzqAreaVo();
            BeanUtils.copyProperties(s, t);
            return t;
        }).collect(Collectors.toList());

        if (fromredis) {
            //获取下面的城市
            for (MqXzqAreaVo s : xzqTrees) {
                List<MqXzqAreaRelation> list = relationService.GetList(s.getAreaId());
                s.setRelations(list);

            }
        }

        /**
         * 获取所有
         */
        if (isAll) {
            //获取下面的分类
            for (MqXzqAreaVo s : xzqTrees) {
                if (s.getRelations() == null || s.getRelations().size() == 0) {
                    List<MqXzqAreaVo> list = xzqAreaTree(s.getAreaCode(), s.getAreaId(), "", true, true);
                    s.setAreas(list);
                }

                if (s.getAreas() == null || s.getAreas().size() == 0) {
                    s.setLeaf(true);
                }
            }
        } else {

            for (MqXzqAreaVo s : xzqTrees) {
                Integer sub = helper.GetSubCount(s.getAreaId());
                if (sub == 0) {
                    s.setLeaf(true);
                } else {
                    s.setLeaf(false);
                }
            }
        }


        if (fromredis) {
            //保存到缓存
            helper.SaveTreeRedis(rediskey, xzqTrees);
        }

        return xzqTrees;
    }

    @Override
    public MqXzqAreaVo DetailById(String xzqAreaId) {

        MqXzqAreaVo xzqAreaVo = new MqXzqAreaVo();
        MqXzqArea area = this.getById(xzqAreaId);
        BeanUtil.copyProperties(area, xzqAreaVo);

        if (area != null) {
            List<MqXzqAreaRelation> list = relationService.GetList(area.getAreaId());
            xzqAreaVo.setRelations(list);
        }

        return xzqAreaVo;

    }

    @Override
    @Transactional
    public MqXzqAreaVo Save(MqXzqAreaVo xzq) {

        if (xzq.getAreaId() == null || xzq.getAreaId().equals("0")) {
            xzq.setAreaId(UUID.fastUUID().toString());
        } else {
            QueryWrapper<MqXzqAreaRelation> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(MqXzqAreaRelation::getXzqAreaId, xzq.getAreaId());
            relationService.remove(queryWrapper);
        }

        this.saveOrUpdate(xzq);

        if (xzq.getRelations() != null) {
            for (MqXzqAreaRelation r : xzq.getRelations()) {
                r.setXzqAreaId(xzq.getAreaId());
            }

            if (xzq.getRelations() != null && xzq.getRelations().size() > 0) {
                relationService.saveBatch(xzq.getRelations());
            }
        }

        return xzq;
    }

    @Override
    @Transactional
    public Boolean Delete(String xzqAreaId) {
        QueryWrapper<MqXzqAreaRelation> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MqXzqAreaRelation::getXzqAreaId, xzqAreaId);
        relationService.remove(queryWrapper);
        return this.removeById(xzqAreaId);
    }
}
